package com.cn.demo.util.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * 线程池
 */
@Slf4j
public class LpThreadPoolExecutor extends ThreadPoolExecutor {

    public LpThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        ThreadFactory threadFactory = this.getThreadFactory();
        if (threadFactory instanceof LpDefaultThreadFactory) {
            //统计线程池的走势
            String poolNamePrefix = ((LpDefaultThreadFactory) threadFactory).getPoolNamePrefix();
//            log.info("SdbThreadPoolUtil poolNamePrefix=[{}],poolsize={},taskcount={}", poolNamePrefix, this.getPoolSize(), this.getTaskCount());
        }
    }

    @Override
    protected void terminated() {
        super.terminated();
        ThreadFactory threadFactory = this.getThreadFactory();
        if (threadFactory instanceof LpDefaultThreadFactory) {
            String poolNamePrefix = ((LpDefaultThreadFactory) threadFactory).getPoolNamePrefix();
            log.info("SdbThreadPoolUtil poolNamePrefix[{}], create largest pool size: {}, completed task count: {}",
                    poolNamePrefix, this.getLargestPoolSize(), this.getCompletedTaskCount());
        }
    }

}